package cn.com.lyj6851.modules.activity.controller;

import cn.com.lyj6851.api.MyShopException;
import cn.com.lyj6851.logging.aop.AopLog;
import cn.com.lyj6851.modules.activity.domain.YdyStoreBargain;
import cn.com.lyj6851.modules.activity.service.YdyStoreBargainService;
import cn.com.lyj6851.modules.activity.service.dto.YdyStoreBargainQueryCriteria;
import cn.com.lyj6851.modules.aop.ForbidSubmit;
import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
*
*/
@Api(tags = "商城:砍价管理")
@RestController
@RequestMapping("api")
public class StoreBargainController {

    private final YdyStoreBargainService yxStoreBargainService;

    public StoreBargainController(YdyStoreBargainService yxStoreBargainService) {
        this.yxStoreBargainService = yxStoreBargainService;
    }

    @AopLog("查询砍价")
    @ApiOperation(value = "查询砍价")
    @GetMapping(value = "/ydyStoreBargain")
    @PreAuthorize("hasAnyRole('admin','STOREBARGAIN_ALL','STOREBARGAIN_SELECT')")
    public ResponseEntity getYdyStoreBargains(YdyStoreBargainQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(yxStoreBargainService.queryAll(criteria,pageable),HttpStatus.OK);
    }



    @AopLog("修改砍价")
    @ApiOperation(value = "修改砍价")
    @PutMapping(value = "/ydyStoreBargain")
    @PreAuthorize("hasAnyRole('admin','STOREBARGAIN_ALL','STOREBARGAIN_EDIT')")
    public ResponseEntity update(@Validated @RequestBody YdyStoreBargain resources){
        if(resources.getBargainMinPrice().compareTo(resources.getBargainMaxPrice()) >= 0){
            throw new MyShopException("单次砍最低价不能高于单次砍最高价");
        }
        if(resources.getMinPrice().compareTo(resources.getPrice()) >= 0){
            throw new MyShopException("允许砍到最低价不能高于砍价金额");
        }
        if(ObjectUtil.isNull(resources.getId())){
            return new ResponseEntity<>(yxStoreBargainService.save(resources),HttpStatus.CREATED);
        }else{
            yxStoreBargainService.saveOrUpdate(resources);
            return new ResponseEntity(HttpStatus.NO_CONTENT);
        }
    }

    @ForbidSubmit
    @AopLog("删除砍价")
    @ApiOperation(value = "删除砍价")
    @DeleteMapping(value = "/ydyStoreBargain/{id}")
    @PreAuthorize("hasAnyRole('admin','STOREBARGAIN_ALL','STOREBARGAIN_DELETE')")
    public ResponseEntity delete(@PathVariable Integer id){
        yxStoreBargainService.removeById(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}
